//
//  MyCenterVC.m
//  WalletAPP
//
//  Created by 罗弟祥 on 2019/10/9.
//  Copyright © 2019 罗弟祥. All rights reserved.
//

#import "MyCenterVC.h"
#import "MyCenterCell.h"
#import "IdentificationVC.h"
#import "PaymentMethedVC.h"
#import "HashrateTeamVC.h"
#import "HashrateNaviVC.h"
#import "SGQRCodeGenerateManager.h"
#import "MyCenterModel.h"
#import "LoginVC.h"
@interface MyCenterVC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *nameL;
@property (weak, nonatomic) IBOutlet UILabel *icodeL;
@property (weak, nonatomic) IBOutlet UILabel *nodeValueL;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIView *codeView;
@property (weak, nonatomic) IBOutlet UIImageView *codeImgV;



@property (nonatomic, retain) NSArray *icoImgArr;
@property (nonatomic, retain) NSArray *titleArr;

@property (nonatomic, retain) RefreshVM *vm;
@property (nonatomic, retain) MyCenterModel *infoModel;


@end

@implementation MyCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    self.view = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyCenterVC class]) owner:self options:nil].firstObject;
    
    self.icoImgArr = @[@"icon_me_item1", @"icon_me_item2", @"icon_me_item3", @"icon_me_item4"];
    self.titleArr = @[@"Identity", @"Hashrate Top", @"Paymengt", @"My Ads"];
    
    self.codeView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    
    self.codeImgV.image = [SGQRCodeGenerateManager generateWithLogoQRCodeData:@"sdfsdf" logoImageName:@"" logoScaleToSuperView:0.5];
    [self setViewsLayer];
    
    [self configViewModel];

}

-(void)setViewsLayer{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.tableView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.tableView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.tableView.layer.mask = maskLayer;
}

-(void)configViewModel{
    
    __weak __typeof(&*self) ws = self;
    self.vm = [RefreshVM new];
    self.vm.urlStr = @"/api/user";
    self.vm.params = [self configParam];
    self.vm.dataClass = [MyCenterModel class];
    [self.vm getRequestDataNormal];
    [self.vm setRequestResultBlock:^(id obj) {
        
        ws.infoModel = obj;
        [ws setViewsValue:ws.infoModel];
    }];
}

-(void)setViewsValue:(MyCenterModel *)model{
    self.nameL.text = model.name;
    self.nodeValueL.text = model.node_address;
    self.icodeL.text = model.icode;
}


- (UIImage *)stringToImage:(NSString *)str{
    NSURL *url = [NSURL URLWithString: str];
    NSData *logoData = [NSData dataWithContentsOfURL:url];
    UIImage * img = [UIImage imageWithData:logoData];
    return img;
}

-(NSDictionary *)configParam{
    return @{};
}


- (IBAction)codeBtnClick:(UIButton *)sender {
    self.codeView.hidden = NO;
    UIImage *image = [self stringToImage:self.infoModel.icode_qrcode];
    if (image) {
        self.codeImgV.image = image;
    }else{
        self.codeImgV.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }


}
- (IBAction)codeViewTapClick:(UITapGestureRecognizer *)sender {
    self.codeView.hidden = YES;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.icoImgArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    MyCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyCenterCell class]) owner:self options:nil].firstObject;
    }
    
    cell.icoImgV.image = [UIImage imageNamed:self.icoImgArr[indexPath.row]];
    cell.titleL.text = self.titleArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        IdentificationVC *vc= [IdentificationVC new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 1){
        HashrateNaviVC *vc= [HashrateNaviVC new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 2){
        PaymentMethedVC *vc= [PaymentMethedVC new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        LoginVC *vc = [LoginVC new];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:navi animated:YES completion:^{
            
        }];
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
